package com_fit.springboot_fit.controller;


import com_fit.springboot_fit.entity.AdminUser;
import com_fit.springboot_fit.service.AdminUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {

    Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private AdminUserService userService;

    // 新增一个测试接口，排除业务逻辑干扰
    @PostMapping("/test")
    public String test(@RequestBody Map<String, String> params) {
        System.out.println("测试接口被调用，参数：" + params);
        return "test success";
    }

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> params) {


        Logger log = LoggerFactory.getLogger(LoginController.class);
        String loginName = params.get("loginName");
        String password = params.get("password");

        log.info("===== 进入 login 方法 =====");
        log.info("loginName" + loginName +"password" + password+"params："+params);



        System.out.println(params);
        Map<String, Object> result = new HashMap<>();

        try {
//            String loginName = params.get("loginName");
//            String password = params.get("password");

            System.out.println(loginName);
            System.out.println(params);
            System.out.println("hihihihihihi");

            // 调用 Service 得到包含 user 和 token 的 Map
//            Map<String, Object> loginResult = userService.login(loginName, params);
//
//            result.put("code", 200);
//            result.put("message", "登录成功");
//            result.put("data", loginResult); // data 中包含 token

        } catch (Exception e) {

//            String loginName = params.get("loginName");
//            String password = params.get("password");

            System.out.println(loginName);

            System.out.println("hihihihihihi");
            result.put("code", 401);
            result.put("message", e.getMessage());
        }

        return result;
    }

    @PostMapping("/logout")
    public Map<String, Object> logout() {
        // 清除安全上下文
        SecurityContextHolder.clearContext();

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "登出成功");
        return result;
    }

    @RequestMapping("/currentUser")
    public Map<String, Object> getCurrentUser() {
        Map<String, Object> result = new HashMap<>();

        try {
            String loginName = SecurityContextHolder.getContext().getAuthentication().getName();
            AdminUser user = userService.findByLoginName(loginName).orElse(null);

            result.put("code", 200);
            result.put("data", user);

        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", e.getMessage());
        }

        return result;
    }
}
